<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <!-- 头部 -->
        <el-tab-pane label="角色管理" lazy>
          <el-button
            type="primary"
            size="small"
            @click="addEvent"
          >+ 新增角色</el-button>
          <el-table :data="dataList">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                <div>{{ (page.page - 1) * page.pagesize + $index + 1 }}</div>
              </template>
            </el-table-column>
            <el-table-column label="角色名 " prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button
                    type="text"
                    @click="permissionClick(row)"
                  >分配权限</el-button>
                  <el-button
                    type="text"
                    @click="editEvent(row)"
                  >修改</el-button>
                  <el-button type="text" @click="delEvent(row)">删除</el-button>
                </div>
              </template></el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="page">
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 3, 10, 100, 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy>
          <Tnfo />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <Add ref="add" @getData="getData" />
    <Permission ref="Permission" :is-show.sync="showPermission" />
  </div>
</template>

<script>
import Add from './components/add.vue'
import { sysRole, sysRoleDelete } from '@/api/setting'
import Permission from './components/setPermission.vue'
export default {
  components: {
    Add,
    Permission,
    Tnfo: () => import('./components/info.vue')
  },
  data() {
    return {
      showPermission: false,
      dataList: [],
      page: {
        page: 1,
        pagesize: 10,
        total: 10
      }
    }
  },
  // 打开页面时渲染页面
  mounted() {
    this.getData()
  },
  methods: {
    // 渲染页面
    async getData() {
      const res = await sysRole(this.page)
      this.page.total = res.data.total
      this.dataList = res.data.rows
      console.log(res.data.rows)
    },
    // 每页的条数
    handleSizeChange(val) {
      this.page.pagesize = val
      this.page.page = 1
      this.getData()
      console.log(`每页 ${val} 条`)
    },
    // 当前页
    handleCurrentChange(val) {
      this.page.page = val
      this.getData()
      console.log(`当前页: ${val}`)
    },
    addEvent() {
      this.$refs.add.isshow = true
      this.$refs.add.mode = 'add'
    },
    // 删除
    delEvent(row) {
      this.$confirm('确定要删除吗？', '删除角色').then(async() => {
        await sysRoleDelete(row.id)
        this.getData()
      })
    },
    // 修改
    editEvent(row) {
      this.$refs.add.isshow = true
      this.$refs.add.mode = 'edit'
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    // 显示分配权限
    permissionClick(row) {
      this.$refs.Permission.getPermission(row)
      this.showPermission = true
    }
  }
}
</script>

<style></style>
